package indi.lddsdu.bean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jack on 18/6/10.
 */
public class Page
{
    private int currentPage = 1;// 当前页数
    private int totalPages = 0;// 总页数
    private int pageSize = 0;// 每页显示数
    private int totalRows = 0;// 总数据数
    private int startNum = 0;// 开始记录
    private int nextPage = 0;// 下一页
    private int previousPage = 0;// 上一页
    private boolean hasNextPage = false;// 是否有下一页
    private boolean hasPreviousPage = false;// 是否有前一页
    private List<Item> items;
    private int showPageSize = 0; //显示多少个超链接页面
    private long timeUsed;
    public static final String TIME_UNIT = "ms";
    public Page() {}

    /**
     * 构造方法，通过每页显示数，总页数求出其他的属性值
     * @param pageSize      每页显示数
     * @param currentPage   当前页数
     * @param totalRows     总数据数
     * @param allItems      代表了所有查询出来的数据
     */
    public Page(int pageSize, int currentPage, int totalRows, long timeUsed, List<Item> allItems)
    {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalRows = totalRows;
        this.timeUsed = timeUsed;
        this.items = new ArrayList<Item>();
        //求解总的显示页数
        if ((totalRows % pageSize) == 0)
        {
            totalPages = totalRows / pageSize;
        } else
        {
            totalPages = totalRows / pageSize + 1;
        }

        //求解是否存在下一页
        if (currentPage >= totalPages)
        {
            hasNextPage = false;
            currentPage = totalPages;
        } else
        {
            hasNextPage = true;
        }

        //求解是否存在上一页
        if (currentPage <= 1)
        {
            hasPreviousPage = false;
            currentPage = 1;
        } else
        {
            hasPreviousPage = true;
        }

        //开始的index
        startNum = (currentPage - 1) * pageSize;
        //下一页的页号
        nextPage = currentPage + 1;

        if (nextPage >= totalPages)
        {
            nextPage = totalPages;
        }

        //上一页页号
        previousPage = currentPage - 1;

        if (previousPage <= 1)
        {
            previousPage = 1;
        }

        showPageSize = Math.min(pageSize, totalRows - startNum);

        for(int i = 0; i < showPageSize; i++){
            items.add(allItems.get((i + startNum)));
        }
    }


    public List<Integer> getShowPageIndexs(){
        List<Integer> showPageIndexs = new ArrayList<>(10);//最多显示9页，使用10页来初始化大小
        int startIndex = Math.max(1, currentPage - 4);

        for (int i = 0; i < 9; i ++){
            int currentIndex = startIndex + i;
            if (currentIndex > totalPages){
                break;
            }
            showPageIndexs.add(currentIndex);
        }
        return showPageIndexs;
    }
//    /**
//     * 这个方法还不了解是什么用
//     */
//    public void reflashPageCode()
//    {
//        this.items = new ArrayList<String>();
//        if (this.totalPages <= this.showPageSize)
//        {
//            for (int i = 1; i <= this.totalPages; i++)
//            {
//                this.items.add(String.valueOf(i));
//            }
//            return;
//        }
//        int middleSide = this.showPageSize >> 1;
//        if (this.currentPage <= middleSide)
//        {
//            for (int i = 1; i <= this.showPageSize; i++)
//            {
//                this.items.add(String.valueOf(i));
//            }
//            this.items.add(String.valueOf(".."));
//            return;
//        }
//        if ((this.totalPages - this.currentPage) <= middleSide)
//        {
//            this.items.add(String.valueOf(".."));
//            for (int i = this.showPageSize - 1; i >= 0; i--)
//            {
//                this.items.add(String.valueOf(this.totalPages - i));
//            }
//            return;
//        }
//        if (middleSide < this.currentPage
//                && this.currentPage - (middleSide + 1) > 0)
//            this.items.add(String.valueOf(".."));
//
//        for (int i = 0; i < this.showPageSize; i++)
//        {
//            this.items.add(String.valueOf((this.currentPage + i)
//                    - middleSide));
//        }
//        if (middleSide > this.currentPage
//                || this.totalPages - (this.currentPage + middleSide) > 0)
//            this.items.add(String.valueOf(".."));
//    }

    public boolean isHasNextPage()
    {
        return hasNextPage;
    }

    public boolean isHasPreviousPage()
    {
        return hasPreviousPage;
    }

    /**
     * @return the nextPage
     */
    public int getNextPage()
    {
        return nextPage;
    }

    /**
     * @param nextPage
     *            the nextPage to set
     */
    public void setNextPage(int nextPage)
    {
        this.nextPage = nextPage;
    }

    /**
     * @return the previousPage
     */
    public int getPreviousPage()
    {
        return previousPage;
    }

    /**
     * @param previousPage
     *            the previousPage to set
     */
    public void setPreviousPage(int previousPage)
    {
        this.previousPage = previousPage;
    }

    /**
     * @return the currentPage
     */
    public int getCurrentPage()
    {
        return currentPage;
    }

    /**
     * @param currentPage
     *            the currentPage to set
     */
    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    /**
     * @return the pageSize
     */
    public int getPageSize()
    {
        return pageSize;
    }

    /**
     * @param pageSize
     *            the pageSize to set
     */
    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    /**
     * @return the totalPages
     */
    public int getTotalPages()
    {
        return totalPages;
    }

    /**
     * @param totalPages
     *            the totalPages to set
     */
    public void setTotalPages(int totalPages)
    {
        this.totalPages = totalPages;
    }

    /**
     * @return the totalRows
     */
    public int getTotalRows()
    {
        return totalRows;
    }

    /**
     * @param totalRows
     *            the totalRows to set
     */
    public void setTotalRows(int totalRows)
    {
        this.totalRows = totalRows;
    }

    /**
     * @param hasNextPage
     *            the hasNextPage to set
     */
    public void setHasNextPage(boolean hasNextPage)
    {
        this.hasNextPage = hasNextPage;
    }

    /**
     * @param hasPreviousPage
     *            the hasPreviousPage to set
     */
    public void setHasPreviousPage(boolean hasPreviousPage)
    {
        this.hasPreviousPage = hasPreviousPage;
    }

    /**
     * @return the startNum
     */
    public int getStartNum()
    {
        return startNum;
    }

    /**
     * @param startNum
     *            the startNum to set
     */
    public void setStartNum(int startNum)
    {
        this.startNum = startNum;
    }

    public List<Item> getItems()
    {
        if (this.items == null) {
            return new ArrayList<Item>();
        }
        return items;
    }

    public void setItems(List<Item> items)
    {
        this.items = items;
    }

    public int getShowPageSize()
    {
        return showPageSize;
    }

    public void setShowPageSize(int showPageSize)
    {
        this.showPageSize = showPageSize;
    }

    public long getTimeUsed() {
        return timeUsed;
    }

    @Override
    public String toString() {
        return "Page{" +
                "currentPage=" + currentPage +
                ", totalPages=" + totalPages +
                ", pageSize=" + pageSize +
                ", totalRows=" + totalRows +
                ", startNum=" + startNum +
                ", nextPage=" + nextPage +
                ", previousPage=" + previousPage +
                ", hasNextPage=" + hasNextPage +
                ", hasPreviousPage=" + hasPreviousPage +
                ", showPageSize=" + showPageSize +
                ", timeUsed=" + timeUsed +
                '}';
    }
}